package com.gitee.feizns.explore.data_structure.array.easy;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 414. 第三大的数
 * @author feizns
 * @since 2020/1/9
 */
public class ThirdMax {

    public static void main(String[] args) {
        System.out.println(hello(null));
    }

    private static boolean hello(Object i) {
        assert i != null : "HELLO";
        return false;
    }

    public static int thirdMax(int[] num) {
        Set<Integer> ret = new HashSet<>();
        ret.add(num[0]);
        int first = 0;
        int second = -1;
        int third = -1;
        for (int i = 1; i < num.length; i++) {
            if ( !ret.contains(num[i]) ) {
                if ( num[i] > num[first] ) {
                    third = second;
                    second = first;
                    first = i;
                } else if ( second == -1 || num[i] > num[second] ) {
                    third = second;
                    second = i;
                } else if ( third == -1 || num[i] > num[third] ) {
                    third = i;
                }
                ret.add(num[i]);
            }
        }
        return third != -1 ? num[third] : num[first];
    }

}
